package com.yzs.mvidemo.demo

import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.FragmentUtils
import com.blankj.utilcode.util.PermissionUtils
import com.chad.library.adapter.base.BaseBinderAdapter
import com.yzs.base.base.BaseDialogFragment
import com.yzs.core.ARouterUrl
import com.yzs.core.base.BaseViewModel
import com.yzs.core.base.CoreBaseFragment
import com.yzs.core.base.CoreBaseVBVMActivity
import com.yzs.core.base.CoreBaseVBVMFragment
import com.yzs.mvidemo.R
import com.yzs.mvidemo.databinding.ActivityMainBinding
import com.yzs.mvidemo.databinding.ActivityRouterBinding
import com.yzs.core.test.bean.NewsItem
import com.yzs.core.utils.ToastUtils
import kotlinx.android.synthetic.main.activity_main.*

@Route(path = ARouterUrl.Demo.URL_DEMO_DEMO)
class DemoActivity : CoreBaseVBVMActivity<ActivityMainBinding, DemoVM>(
    ActivityMainBinding::inflate,
    DemoVM::class
) {

    private val newsRvAdapter by lazy {
        BaseBinderAdapter()
    }

    val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
        // Handle the returned Uri
    }

    override fun init(savedInstanceState: Bundle?) {
        initView()
//        setPageStateLoadingUI(viewModel.liveDataViewStates)

//        viewModel.dispatch()
//        TestDialog().show(supportFragmentManager)

        FragmentUtils.replace(supportFragmentManager, MainFragment(), R.id.fl_layout)
        PermissionUtils.permission(PermissionConstants.STORAGE)
            .callback { isAllGranted, granted, deniedForever, denied ->
                getContent.launch("image/*")
            }.request()
    }

    override fun getLayoutId() = R.layout.activity_main


    override fun initViewModel() {
        super.initViewModel()
//        viewModel = getActivityViewModel(MainVM::class.java)
    }

    override fun observe() {
        vm.liveData.observeKt {
            ToastUtils.showShort("$it")
        }
    }

    private fun initView() {
        rvNewsHome.adapter = newsRvAdapter
        newsRvAdapter.addItemBinder(NewsItem::class.java, DemoRvAdapter())
        newsRvAdapter.setList(
            listOf(
                NewsItem("1", "", ""),
                NewsItem("2", "", ""),
                NewsItem("3", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
                NewsItem("4", "", ""),
            )
        )
    }
}


class TestDialog : BaseDialogFragment() {

    private lateinit var vm: TestDialogVM
    override fun getLayoutId() = R.layout.activity_main

    override fun init(view: View, savedInstanceState: Bundle?) {
    }

    override fun initViewModel() {
        vm = getActivityViewModel(TestDialogVM::class.java)
    }

}


class TestDialogVM : BaseViewModel() {

}


class TestFragment : CoreBaseFragment() {
    override fun init(view: View, savedInstanceState: Bundle?) {

    }

    override fun getLayoutId() = R.layout.activity_router

}

class MainFragment : CoreBaseVBVMFragment<ActivityRouterBinding, DemoVM>(
    ActivityRouterBinding::inflate,
    DemoVM::class
) {
    //    private val viewModel: MainVM by viewModels()
    override fun init(view: View, savedInstanceState: Bundle?) {
    }

    override fun getLayoutId() = R.layout.activity_router

    override fun observe() {
        setPageStateLoadingUI(vm.liveDataViewStates)
        vm.dispatch()
    }
}